package me.leon.mylibs.DB;

import android.content.Context;

import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.rx.RealmObservableFactory;

/**
 * @author PC
 * @date 2017/12/19
 */

public class RealmManager {
    private static RealmManager INSTANCE;

    private RealmManager() {
    }

    public static RealmManager getInstance() {
        if (INSTANCE == null) {
            synchronized (RealmManager.class) {
                if (INSTANCE == null) {
                    INSTANCE = new RealmManager();
                }
            }
        }
        return INSTANCE;
    }

    public void init(Context ctx, String dbName, int version) {
        Realm.init(ctx);
        RealmConfiguration config = new RealmConfiguration.Builder()
                .name(dbName)
//                .assetFile()  //从assert目录copy
                .deleteRealmIfMigrationNeeded()
                .schemaVersion(version)
//                .directory() //默认data/data/包名/files目录下
                .rxFactory(new RealmObservableFactory())
                .encryptionKey("key1234561key1234561key1234561key1234561key1234561key1234561haha".getBytes())//64位秘钥
                .build();

        Realm.setDefaultConfiguration(config);
    }


}
